<template>
  <div class="contents">
    <div class="f1">商务信用报告</div>
    <div class="f2">
      <!-- 搜索栏 -->
      <div class="user-search">
        <div class="search-input">
          <span>企业名称</span>
          <input
            maxlength="18"
            type="text"
            placeholder="请输入企业名称/统一社会信用代码"
            v-model="entname"
          />
        </div>
        <div>
          <span>报告类型</span>
          <el-select v-model="reporttype" placeholder="请选择">
            <el-option label="基础版" value="3"></el-option>
            <el-option label="标准版" value="2"></el-option>
          </el-select>
        </div>
        <div>
          <span>报告状态</span>
          <el-select v-model="status" placeholder="请选择">
            <el-option label="生成中" value="1"></el-option>
            <el-option label="生成成功" value="3"></el-option>
            <el-option label="生成失败" value="4"></el-option>
          </el-select>
        </div>
      </div>
      <!-- 按钮 -->
      <div class="f2_bottom">
        <button @click="inquire">查询</button>
        <button @click="reset" class="special">重置</button>
      </div>
    </div>

    <div class="table">
      <el-table
        :data="userData"
        border
        style="width: 100%"
        :header-cell-style="{
          background: '#F1F5FF',
          color: '#4D4D4D'
        }"
      >
        <el-table-column
          prop="num"
          label="序号"
          fixed
          min-width="80"
          align="center"
        ></el-table-column>
        <el-table-column prop="orderid" label="订单号" min-width="180" align="center">
        </el-table-column>
        <el-table-column prop="entname" label="企业名称" min-width="340" align="center">
        </el-table-column>
        <el-table-column prop="codeid" label="统一社会信用代码" min-width="240" align="center">
        </el-table-column>
        <el-table-column
          prop="version"
          label="报告类型"
          align="center"
          min-width="120"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <span v-show="scope.row.version == 2">标准版</span>
            <span v-show="scope.row.version == 3">基础版</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="phone"
          label="手机号"
          align="center"
          min-width="150"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.phone | getData }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="status"
          label="状态"
          min-width="140"
          align="center"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <span v-show="scope.row.status == 2 || scope.row.status == 1" style="color: #888"
              >生成中</span
            >
            <span v-show="scope.row.status == 3" style="color: #63a103">生成成功</span>
            <span v-show="scope.row.status == 4" style="color: #d9001b">生成失败</span>
          </template>
        </el-table-column>
        <el-table-column prop="inputdate" label="查询时间" min-width="140" align="center">
          <template slot-scope="scope">{{ scope.row.inputdate | getDate }}</template>
        </el-table-column>
        <!-- <el-table-column prop="inputdate" label="生成时间" min-width="140" align="center">
          <template slot-scope="scope">{{ scope.row.inputdate | getDate }}</template>
        </el-table-column> -->
        <el-table-column prop="address" label="操作" align="center" min-width="120" fixed="right">
          <template slot-scope="scope">
            <span
              size="mini"
              type="text"
              @click="download(scope.row.id, scope.row.status, userData, scope.$index)"
              class="span"
              >下载</span
            >
            <!-- <span
              size="mini"
              type="text"
              @click="delReport(scope.row.id)"
              class="span"
              style="color: #ff4040; border-bottom: 0.0052rem solid #ff4040"
              >删除</span
            > -->
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <div v-if="userData" class="pagination">
        <el-pagination
          background
          layout="prev, pager, next"
          @next-click="nextClick()"
          @prev-click="prevClick()"
          @current-change="handlePageChange"
          :total="pages * pageSize"
          :current-page.sync="pageNum"
        ></el-pagination>
        <span>共{{ pages }}页</span>
      </div>
    </div>
  </div>
</template>

<script>
import { myPostFetch, downloadReport, delBussinessReport } from '@/api/report'
export default {
  data() {
    return {
      entname: '', //订单编号
      reporttype: '', //报告类型
      status: '', //报告状态
      pageNum: 1, //当前页数
      pageSize: 10, //每页显示条数
      pages: 1, //总页数
      total: 1,
      userData: null
    }
  },
  // 过滤器
  filters: {
    getArea: function (value) {
      value = parseFloat(value)
      if (value == 0) return '-'
      if (!value) return '-'
      // 将数值截取，保留两位小数
      value = value.toFixed(2)
      // 获取整数部分
      const intPart = Math.trunc(value)
      // 整数部分处理，增加,
      const intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
      // 预定义小数部分
      let floatPart = '.00'
      // 将数值截取为小数部分和整数部分
      const valueArray = value.toString().split('.')
      if (valueArray.length === 2) {
        // 有小数部分
        floatPart = valueArray[1].toString() // 取得小数部分
        return intPartFormat + '.' + floatPart
      }
      return intPartFormat + floatPart
    },
    getDate: function (value) {
      if (!value) return '-'
      if (value == 'null' || value == null || value == '' || value == ' ') return '-'
      let date = value.split(' ')[0]
      return date
    },
    getData: function (value) {
      if (value === 0) return 0
      if (value == ' ' || value == null || value == 'null' || value == '') return '-'
      return value
    }
  },

  mounted() {
    this.getInviteList()
  },
  methods: {
    // 下载
    download(id, status, data, index) {
      if (status == 1 || status == 2) {
        this.$Message.warning('报告生成中，请稍后')
      } else if (status == 3) {
        downloadReport({
          id: id
        })
          .then(success => {
            let imgUrlStr = success.data.file
            const a = document.createElement('a') // 动态创建a标签，防止下载大文件时，用户没看到下载提示连续点击
            a.setAttribute('target', '_blank')
            a.download = data[index].file.split('/')[5].split('.')[0]
            a.href = imgUrlStr
            a.click()
          })
          .catch(() => {
            this.$Message.error('报告下载失败')
          })
      } else {
        this.$Message.error('报告生成失败')
      }
    },
    //删除报告
    delReport(id) {
      this.$confirm('确定删除当前报告?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delBussinessReport({
            id: id
          })
          this.$Message({
            type: 'success',
            message: '报告删除成功!'
          })
          this.getInviteList()
        })
        .catch(() => {})
    },
    // 请求接口获取白名单列表
    async getInviteList() {
      let { entname, reporttype, status, pageNum, pageSize } = this
      let url = '/zb/getBusinessReport'
      let params = {
        entname: entname,
        reporttype: reporttype,
        status: status,
        pageNum: pageNum,
        pageSize: pageSize,
        codeid: ''
      }
      let data = await myPostFetch(url, params)
      if (data.code == 500) return this.$Message.error(data.msg)
      if (data.data.list.length == 0) {
        this.userData = null
        return
      }
      this.userData = data.data.list
      this.pages = data.data.pages
      for (let i = 0; i < this.userData.length; i++) {
        this.userData[i].num = (this.pageNum - 1) * 10 + i + 1
      }
      return data
    },
    // 查询按钮
    inquire() {
      this.pageNum = 1
      localStorage.clear()
      this.getInviteList()
    },
    // 点击数字改变page的值
    handlePageChange(currentPage) {
      this.pageNum = currentPage
      this.getInviteList()
    },
    // 点击下一页
    nextClick() {
      this.pageNum += 1
    },
    // 点击上一页
    prevClick() {
      this.pageNum -= 1
    },
    // 重置
    reset() {
      this.entname = ''
      this.reporttype = ''
      this.status = ''
      this.pageNum = 1
      localStorage.clear()
      this.getInviteList()
    }
  }
}
</script>

<style lang="scss" scoped>
.contents {
  box-sizing: border-box;
  padding: 0.0781rem 0.1823rem;
  .f1 {
    box-sizing: border-box;
    display: flex;
    align-items: center;
    height: 0.2083rem;
    color: #222;
    font-size: 0.0933rem;
    font-weight: 600;
    border-bottom: 0.0052rem solid #999;
  }
  .f2 {
    background: #f5f7f9;
    box-sizing: border-box;
    padding-bottom: 0.0781rem;
    margin-bottom: 0.1042rem;
    // 搜索栏
    .user-search {
      position: relative;
      display: flex;
      align-items: center;
      justify-content: space-around;
      width: 100%;
      height: 0.4167rem;
      // background: #ffffff;
      margin-top: 0.0781rem;
      span {
        font-size: 0.0833rem;
        font-family: Source Han Sans SC;
        font-weight: 400;
        color: #333;
        margin: 0 0.0521rem 0 0.1042rem;
      }
      .search-input {
        display: flex;
        cursor: pointer;
        position: relative;
        align-items: center;
        // width: 1.3021rem;
        height: 0.2083rem;
        // margin-left: 15px;
        input {
          box-sizing: border-box;
          width: 1.3021rem;
          height: 0.2031rem;
          font-size: 0.0781rem;
          border: 0.0052rem solid #e0e0e0;
          color: #606266;
          outline: 0;
          padding-left: 0.026rem;
          &::placeholder {
            color: #aaa;
            font-size: 0.0781rem;
          }
        }
      }
    }
    // 按钮
    .f2_bottom {
      display: flex;
      justify-content: center;
      button {
        width: 0.5208rem;
        height: 0.1563rem;
        margin-left: 0.0781rem;
        color: #ffffff;
        background: #3f6ef2;
      }
      .special {
        color: #3f6ef2;
        background: #fff;
        border: 0.0052rem solid #3f6ef2;
      }
    }
  }
  .f3 {
    position: relative;
    height: 0.2604rem;
    margin-top: 0.0781rem;
    .btn {
      position: absolute;
      right: 0;
      button {
        width: 0.5208rem;
        height: 0.1563rem;
        margin-left: 0.0781rem;
        color: #ffffff;
        background: #3f6ef2;
      }
      .special {
        color: #3f6ef2;
        background: #fff;
        border: 0.0052rem solid #3f6ef2;
      }
    }
  }
  // .f4 {
  // 表格
  .table {
    background-color: #fff;
    cursor: pointer;
    .span {
      display: inline-block;
      width: 0.1563rem;
      height: 0.125rem;
      font-size: 0.0781rem;
      font-weight: 400;
      color: #3f6ef2;
      border-bottom: 0.0052rem solid #3f6ef2;
      cursor: pointer;
      margin-right: 0.0625rem;
    }
    .isdaishenpi {
      display: flex;
      justify-content: center;
      align-items: center;
      p {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 0.0625rem;
        width: 0.2344rem;
        height: 0.1042rem;
        color: #0052d9;
        background: #cddffd;
        border-radius: 0.0104rem;
        margin-left: 0.026rem;
      }
    }
    .state-bar {
      span {
        font-size: 0.0833rem;
        font-family: Source Han Sans SC;
        font-weight: 400;
        color: #606266;
        margin-right: 0.0521rem;
      }
    }
    // 分页器
    .pagination {
      display: flex;
      align-items: center;
      padding: 0.1rem 0.1563rem 0.0521rem 0.0521rem;
      justify-content: right;
      span {
        font-size: 0.0833rem;
        font-family: Source Han Sans SC;
        font-weight: 400;
        color: #666666;
        margin-top: 0.0104rem;
      }
    }
  }
}

/* 输入框样式  */
::v-deep .el-input__inner {
  box-sizing: border-box;
  width: 1.25rem;
  height: 0.2031rem;
  font-size: 0.0781rem;
  border: 0.0052rem solid #e0e0e0;
  border-radius: 0px;
  color: #606266;
  padding-left: 0.026rem;
  outline: 0;
  &::placeholder {
    color: #aaa;
    font-size: 0.0781rem;
  }
}

/* 表格 */
::v-deep .el-table {
  th.el-table__cell > .cell {
    font-size: 0.0833rem;
    font-weight: 400;
  }
  .cell {
    font-size: 0.0833rem;
  }
  .el-table__body-wrapper {
    overflow: auto;
    height: calc(100% - 85px);

    &::-webkit-scrollbar {
      width: 0; /* 纵向滚动条 宽度 */
      height: 0.0781rem; /* 横向滚动条 高度 */
    }
    &::-webkit-scrollbar-thumb {
      width: 0.0521rem; /* 横向滚动条 宽度 */
      height: 0.0781rem; /* 纵向滚动条 高度 */
      background-color: rgb(167, 164, 164);
    }
  }
}
::v-deep .el-tag {
  height: 0.1667rem;
  width: 0.3646rem;
  font-size: 0.0625rem;
  line-height: 0.1563rem;
}
</style>

<style lang="scss">
.el-pagination.is-background .el-pager li {
  border-radius: 0.1042rem;
}
.el-pagination.is-background .el-pager li:not(.disabled).active {
  background-color: #3f6ef2;
}
.el-pagination {
  font-size: 0.0833rem;
  font-family: Source Han Sans SC;
  font-weight: 400;
  color: #666666;
}
.el-pagination.is-background .btn-next,
.el-pagination.is-background .btn-prev,
.el-pagination.is-background .el-pager li {
  background-color: transparent;
}

.el-table tr {
  height: 0.2604rem;
}
.el-table thead {
  height: 0.2604rem;
}
</style>
